package com.sky.service.admin.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.exception.BaseException;
import com.sky.mapper.admin.CategoryMapper;
import com.sky.mapper.admin.DishMapper;
import com.sky.mapper.admin.SetmealDishMapper;
import com.sky.mapper.admin.SetmealMapper;
import com.sky.pojo.Dish;
import com.sky.pojo.Setmeal;
import com.sky.pojo.SetmealDish;
import com.sky.result.PageResult;
import com.sky.service.admin.SetmealService;
import com.sky.vo.SetmealVO;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.List;

/**
 * @author ljk74
 */
@Service
@RequiredArgsConstructor
public class SetmealServiceImpl implements SetmealService {
    private final SetmealMapper setmealMapper;
    private final CategoryMapper categoryMapper;
    private final SetmealDishMapper setmealDishMapper;
    private final DishMapper dishMapper;

    @Override
    @Transactional
    @CacheEvict(value = "Setmeal",allEntries = true)
    public void addSetmeal(SetmealDTO setmealDTO) {
        /*- 套餐名称唯一
        - 套餐必须属于某个分类
        - 套餐必须包含菜品
        - 名称、分类、价格、图片为必填项
        - 添加菜品窗口需要根据分类类型来展示菜品
        - 新增的套餐默认为停售状态*/
        if (categoryMapper.findById(setmealDTO.getCategoryId()) == null) {
            throw new BaseException(MessageConstant.CATEGORY_NOT_FOUND);
        }
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmeal.setStatus(StatusConstant.DISABLE);
        setmealMapper.insertSetmeal(setmeal);

        insertSetmealDishes(setmealDTO, setmeal.getId());
    }

    @Override
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        setmealPageQueryDTO.check();
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealPageQueryDTO, setmeal);
        Page<SetmealVO> list = setmealMapper.pageQuery(setmeal);
        return new PageResult(list.getTotal(), list.getResult());
    }

    @Override
    @Transactional
    @CacheEvict(value = "Setmeal",allEntries = true)
    public void delSetmeal(List<Long> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return;
        }
        //起售中的套餐不能删除
        List<Setmeal> checkList = setmealMapper.selectByIds(ids, StatusConstant.ENABLE);
        if (!CollectionUtils.isEmpty(checkList)) {
            throw new BaseException(MessageConstant.SETMEAL_ON_SALE);
        }
        setmealMapper.delSetmeal(ids);
        setmealDishMapper.delBySetmealId(ids);
    }

    @Override
    public SetmealVO findById(Long setmealId) {
        Setmeal setmeal = setmealMapper.selectById(setmealId);
        if (setmeal == null) {
            throw new BaseException(MessageConstant.SETMEAL_NOT_FOUND);
        }
        List<SetmealDish> dishList = setmealDishMapper.selectBySetmealId(setmealId);
        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal, setmealVO);
        setmealVO.setSetmealDishes(dishList);
        return setmealVO;
    }

    @Override
    @Transactional
    @CacheEvict(value = "Setmeal",allEntries = true)
    public void updateSetmeal(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        if (StatusConstant.ENABLE.equals(setmeal.getStatus())) {
            throw new BaseException(MessageConstant.DISH_ON_SALE);
        }
        setmealMapper.update(setmeal);
        //删除套餐菜品表
        setmealDishMapper.delBySetmealId(Collections.singletonList(setmealDTO.getId()));
        insertSetmealDishes(setmealDTO, setmealDTO.getId());
    }

    @Override
    @CacheEvict(value = "Setmeal",allEntries = true)
    public void startOrStop(Integer status, Long id) {
        //- 可以对状态为起售的套餐进行停售操作，可以对状态为停售的套餐进行起售操作
        //- 起售的套餐可以展示在用户端，停售的套餐不能展示在用户端
        //- 起售套餐时，如果套餐内包含停售的菜品，则不能起售
        if (StatusConstant.ENABLE.equals(status)){
            List<Dish> dishList = dishMapper.selectBySetmealId(id);
            for (Dish dish : dishList) {
                if (StatusConstant.DISABLE.equals(dish.getStatus())){
                    throw new BaseException(MessageConstant.SETMEAL_ENABLE_FAILED);
                }
            }
        }
        Setmeal setmeal = Setmeal.builder()
                .id(id)
                .status(status)
                .build();
        setmealMapper.update(setmeal);
    }

    private void insertSetmealDishes(SetmealDTO setmealDTO, Long id) {
        Long setmealId = id;
        List<SetmealDish> list = setmealDTO.getSetmealDishes();
        if (!CollectionUtils.isEmpty(list)) {
            for (SetmealDish setmealDish : list) {
                setmealDish.setSetmealId(setmealId);
            }
            setmealDishMapper.insertSetmealDish(list);
        } else {
            throw new BaseException(MessageConstant.DISH_NOT_FOUND);
        }
    }
}
